package com.rickpan.dto.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rickpan.entity.WorkRecord;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 工作记录响应DTO
 */
public class WorkRecordResponse {

    private Long id;
    private Long userId;

    @JsonFormat(pattern = "yyyy-MM-dd")
    private LocalDate date;

    private String title;
    private String content;
    private Long categoryId;
    private String categoryName;
    private String categoryColor;
    private String categoryIcon;
    private WorkRecord.Status status;
    private String statusDescription;
    private WorkRecord.Priority priority;
    private String priorityDescription;
    private Integer timeSpent;
    private List<String> tags;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updatedAt;

    // 构造函数
    public WorkRecordResponse() {}

    public WorkRecordResponse(WorkRecord workRecord) {
        this.id = workRecord.getId();
        this.userId = workRecord.getUserId();
        this.date = workRecord.getDate();
        this.title = workRecord.getTitle();
        this.content = workRecord.getContent();
        this.categoryId = workRecord.getCategoryId();
        this.status = workRecord.getStatus();
        this.statusDescription = workRecord.getStatus().getDescription();
        this.priority = workRecord.getPriority();
        this.priorityDescription = workRecord.getPriority().getDescription();
        this.timeSpent = workRecord.getTimeSpent();
        this.createdAt = workRecord.getCreatedAt();
        this.updatedAt = workRecord.getUpdatedAt();
        
        // 处理tags JSON字符串
        if (workRecord.getTags() != null && !workRecord.getTags().isEmpty()) {
            try {
                // 使用Jackson解析JSON字符串为List
                ObjectMapper mapper = new ObjectMapper();
                this.tags = mapper.readValue(workRecord.getTags(), new TypeReference<List<String>>() {});
            } catch (Exception e) {
                // 如果解析失败，尝试作为单个标签处理
                this.tags = List.of(workRecord.getTags());
            }
        } else {
            this.tags = List.of();
        }
    }

    // Getter 和 Setter 方法
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public LocalDate getDate() {
        return date;
    }

    public void setDate(LocalDate date) {
        this.date = date;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Long getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Long categoryId) {
        this.categoryId = categoryId;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public String getCategoryColor() {
        return categoryColor;
    }

    public void setCategoryColor(String categoryColor) {
        this.categoryColor = categoryColor;
    }

    public String getCategoryIcon() {
        return categoryIcon;
    }

    public void setCategoryIcon(String categoryIcon) {
        this.categoryIcon = categoryIcon;
    }

    public WorkRecord.Status getStatus() {
        return status;
    }

    public void setStatus(WorkRecord.Status status) {
        this.status = status;
        this.statusDescription = status != null ? status.getDescription() : null;
    }

    public String getStatusDescription() {
        return statusDescription;
    }

    public void setStatusDescription(String statusDescription) {
        this.statusDescription = statusDescription;
    }

    public WorkRecord.Priority getPriority() {
        return priority;
    }

    public void setPriority(WorkRecord.Priority priority) {
        this.priority = priority;
        this.priorityDescription = priority != null ? priority.getDescription() : null;
    }

    public String getPriorityDescription() {
        return priorityDescription;
    }

    public void setPriorityDescription(String priorityDescription) {
        this.priorityDescription = priorityDescription;
    }

    public Integer getTimeSpent() {
        return timeSpent;
    }

    public void setTimeSpent(Integer timeSpent) {
        this.timeSpent = timeSpent;
    }

    public List<String> getTags() {
        return tags;
    }

    public void setTags(List<String> tags) {
        this.tags = tags;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
}
